import aiohttp
import asyncio
from config import app


class AsyncRequest(object):
    def __init__(
            self,
            url=None,
            url_list=[],
            method='GET',
            headers={},
            post_data={},
            encoding='utf-8',
            parse_func=None,
            parse_method="xpath"):

        self.url = url
        self.url_list = url_list
        self.method = method
        self.headers = headers
        self.post_data = post_data
        self.encoding = encoding
        self.parse_func = parse_func
        self.parse_method = parse_method

    async def fetch(self, client, url):
        allow_methods = ['GET', 'POST']
        if self.method not in allow_methods:
            print("请求应该为GET或者POST方法")
            return
        async with client.request(self.method, url, headers=self.headers, data=self.post_data) as resp:
            assert resp.status == 200
            return await resp.text(self.encoding)

    async def send_request(self, url):
        if url is not None:
            async with aiohttp.ClientSession() as client:
                try:
                    content = await self.fetch(client, url)
                    self.parse_func(content, self.parse_method, url) if self.parse_func else None
                except Exception as e:
                    print(f"{url}请求失败",e)

    def run(self):
        loop = asyncio.get_event_loop()
        self.url and loop.run_until_complete(self.send_request(self.url))
        tasks = [self.send_request(url) for url in self.url_list]
        loop.run_until_complete(asyncio.wait(tasks)) if tasks else None


if __name__ == '__main__':
    a = AsyncRequest(url_list=["https://www.baidu.com/", "https://www.baidu.com/"])
    a.run()
